<template>
  <div>
    <h1>通知公告</h1>
    <el-form :inline="true" :model="notifis" class="demo-form-inline">
      <el-form-item label="title">
        <el-input v-model="notifis.title" placeholder="title"></el-input>
      </el-form-item>
      <el-form-item label="username">
        <el-input v-model="notifis.username" placeholder="title"></el-input>
      </el-form-item>
      <el-form-item label="type">
        <el-select v-model="notifis.type" placeholder="type">
          <el-option label="公告" value="1"></el-option>
          <el-option label="通知" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="createTime">
        <el-date-picker
            v-model="times"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button>
        <el-button type="primary" @click="clear">重置</el-button>
        <el-button type="primary" @click="openAdd">添加</el-button>
        <el-button type="primary" @click="delAll">批删</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        border
        style="width: 100%"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          fixed
          prop="id"
          label="id"
          width="150">
      </el-table-column>
      <el-table-column
          prop="title"
          label="title"
          width="120">
      </el-table-column>
      <el-table-column
          prop="type"
          label="type"
          width="120">
        <template v-slot="s">
          <el-tag v-if="s.row.type==1">公告</el-tag>
          <el-tag v-if="s.row.type==2">通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="status"
          label="status"
          width="120">
        <template v-slot="s">
          <el-tag v-if="s.row.status==0">正常</el-tag>
          <el-tag v-if="s.row.status==1">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="username"
          label="username"
          width="120">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="createTime"
          width="120">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="delById(scope.row.id)" type="text" size="small">删除</el-button>
          <el-button @click="openUpdate(scope.row)" type="text" size="small">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @current-change="goPage"
        :page-size="size"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="title" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="type" :label-width="formLabelWidth">
          <el-select v-model="form.type" placeholder="type">
            <el-option label="公告" value="1"></el-option>
            <el-option label="通知" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="status" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="status">
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="notifisAdd">确 定</el-button>
      </div>
    </el-dialog>



    <el-dialog title="添加" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="title" :label-width="formLabelWidth1">
          <el-input v-model="form1.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="type" :label-width="formLabelWidth1">
          <el-select v-model="form1.type" placeholder="type">
            <el-option label="公告" value="1"></el-option>
            <el-option label="通知" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="status" :label-width="formLabelWidth1">
          <el-select v-model="form1.status" placeholder="status">
            <el-option label="正常" value="0"></el-option>
            <el-option label="异常" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateById">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "NotifisView",
  data() {
    return {
      tableData:[],
      size:3,
      total:0,
      current:1,
      notifis:{},
      times:[],
      dialogFormVisible:false,
      formLabelWidth:'120px',
      form:{},
      username:'',
      ids:[],
      dialogFormVisible1:false,
      formLabelWidth1:'120px',
      form1:{},
    }
  }, methods: {
    updateById(){
      this.axios.post("/notifis/updateById",this.form1).then(()=>{
        this.$message.success("修改成功");
        this.dialogFormVisible1 = false;
        this.list();
      })
    },
    openUpdate(row){
      this.form1 = row;
      this.dialogFormVisible1 = true
    },
    delById(id){
      this.axios.post("/notifis/delById?id="+id).then(()=>{
        this.$message.success("删除成功");
        this.list();
      })
    },
    delAll(){
      this.axios.post("/notifis/delAll?ids="+this.ids).then(()=>{
        this.$message.success("删除成功");
        this.list();
      })
    },
    handleSelectionChange(rows){
      console.log(rows);
      this.ids = rows.map(item=>item.id).join(",");
      console.log(this.ids)
    },
    notifisAdd(){
      this.username = localStorage.getItem("username");
      console.log(this.username)
      this.axios.post("/notifis/notifisAdd",this.form,{params:{username:this.username}}).then(res=>{
        if(res.data.code==200){
          this.$message.success("添加成功");
          this.dialogFormVisible = false;
          this.list();
        }else if(res.data.code==500){
          this.$message.error("禁止重复提交")
        }

      })
    },
    openAdd(){
      this.dialogFormVisible = true;
    },
    clear(){
      location.reload();
    },
    goPage(page){
      this.current = page;
      this.list();
    },
    list(){
      this.notifis.d1 = this.times[0];
      this.notifis.d2 = this.times[1];
      this.axios.post("/notifis/notifisList?current="+this.current+"&size="+this.size,this.notifis).then(res=>{
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      })
    }
  }, created() {
    this.list();
  }
}
</script>

<style scoped>

</style>
